import os
from pathlib import Path
import cv2

class Config:
    def __init__(self):
        self.BASE_DIR = Path(__file__).resolve().parent.parent
        
        # 文件存储配置
        self.UPLOAD_FOLDER = self.BASE_DIR / "static" / "uploads"
        self.PROCESSED_FOLDER = self.BASE_DIR / "static" / "processed"
        
        # 确保目录存在
        os.makedirs(self.UPLOAD_FOLDER, exist_ok=True)
        os.makedirs(self.PROCESSED_FOLDER, exist_ok=True)
        
        # 允许的文件扩展名
        self.ALLOWED_EXTENSIONS = {
            "image": ["jpg", "jpeg", "png"],
            "video": ["mp4", "avi"]
        }
        
        # 人脸检测模型路径
        self.FACE_CASCADE_PATH = self._get_face_cascade_path()
        
        # PaddleOCR配置
        self.OCR_CONFIG = {
            "use_angle_cls": False,
            "lang": "ch",
            "det_limit_side_len": 960,
            "det_db_thresh": 0.3,
            "det_db_box_thresh": 0.5,
            "use_gpu": False  # 确保在CPU上运行
        }
    
    def _get_face_cascade_path(self):
        """获取人脸检测模型文件路径"""
        # 尝试从OpenCV数据目录加载
        cascade_path = Path(cv2.data.haarcascades) / "haarcascade_frontalface_default.xml"
        if cascade_path.exists():
            return str(cascade_path)
        
        # 尝试从项目目录加载
        project_path = self.BASE_DIR / "haarcascade_frontalface_default.xml"
        if project_path.exists():
            return str(project_path)
        
        raise FileNotFoundError("无法找到人脸检测模型文件")

# 全局配置实例
config = Config()